package com.digital.smartgrow.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digital.smartgrow.common.util.BeanCopyUtil;
import com.digital.smartgrow.common.util.WebTool;
import com.digital.smartgrow.dto.business.UpJobLogPageDTO;
import com.digital.smartgrow.entity.business.UpBase;
import com.digital.smartgrow.entity.business.UpJobLog;
import com.digital.smartgrow.dao.business.UpJobLogMapper;
import com.digital.smartgrow.entity.business.UpProcessLog;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.service.business.UpBaseService;
import com.digital.smartgrow.service.business.UpJobLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.service.business.UpProcessLogService;
import com.digital.smartgrow.service.sysinfo.DictionaryService;
import com.digital.smartgrow.vo.businesscenter.MeUpJobInfoVo;
import com.digital.smartgrow.vo.businesscenter.UpJobVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 职评明细表 服务实现类
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-13
 */
@Service
public class UpJobLogServiceImpl extends ServiceImpl<UpJobLogMapper, UpJobLog> implements UpJobLogService {
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private UpProcessLogService processLogService;
    @Autowired
    private UpBaseService baseService;
    @Override
    public Object getListByPage(UpJobLogPageDTO pageDTO) {
        Page<UpJobLog> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<UpJobLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(pageDTO.getUpLevel()), "upLevel", pageDTO.getUpLevel());
        queryWrapper.eq(pageDTO.getStatus()!=null, "status", pageDTO.getStatus());
        if (StringUtils.isNotBlank(pageDTO.getKeywords())) {
            queryWrapper
                    .and(
                            wrapper ->
                                    wrapper.like("name", pageDTO.getKeywords()).
                                            or().like("userCode", pageDTO.getKeywords()).
                                            or().like("userName", pageDTO.getKeywords()).
                                            or().like("gender", pageDTO.getKeywords()).
                                            or().like("faculty", pageDTO.getKeywords()).
                                            or().like("series", pageDTO.getKeywords()).
                                            or().like("major", pageDTO.getKeywords()).
                                            or().like("upPost", pageDTO.getKeywords())
                    );
        }
        return this.page(page, queryWrapper);
    }

    @Override
    public Object getInfo(Integer id) {
        return this.getById(id);
    }

    @Override
    public Object getListByPage(Integer pageNum, Integer pageSize, String keyword, String upLevel, Integer status) {
        Page<UpJobLog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<UpJobLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(upLevel), "name", upLevel);
        queryWrapper.eq(StringUtils.isNotBlank(upLevel), "status", status);
       // 姓名、工号、院系74、系列67、职务68、学科73
        //院系
        String faculty =StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(74, keyword):null;
        //系列
        String series = StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(67, keyword):null;
        //职务
        String upPost = StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(68, keyword):null;
        //学科
        String major = StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(73, keyword):null;

        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper
                    .and(
                    wrapper ->
                            wrapper.like("userName", keyword).
                                    or().like("userCode", keyword).
                                    or().like("faculty", faculty).
                                    or().like("series", series).
                                    or().like("upPost", upPost).
                                    or().like("major", major)
            );
        }


        Page<UpJobLog> page1 = this.page(page, queryWrapper);
        List<UpJobLog> records = page1.getRecords();
        List<UpJobLog> upJobLogs = BeanCopyUtil.copyListProperties(records, UpJobLog::new);
        //查询字段项目有没有需要自己审核的项目
        Teacher user = WebTool.getCurrentUser();
        if(user.getId()!=0){
            for(int i=0;i<upJobLogs.size();i++) {
                Integer id = upJobLogs.get(i).getId();
                List<UpProcessLog> processLogList = processLogService.lambdaQuery()
                        .eq(UpProcessLog::getUpJobLogId, id)
                        .eq(UpProcessLog::getCheckId, user.getId())
                        .list();
                if(processLogList.size()<=0){
                    upJobLogs.remove(i);
                    i=i-1;
                }
            }
        }
        if(upJobLogs.size()>0){
            page1.setRecords(upJobLogs);
            return page1;
        }
       return "无审核数据";
    }

    /**
     * 审核
     * @param id
     * @return
     */
    @Override
    public Object checkInfo(Integer id) {
        //Teacher user = WebTool.getCurrentUser();
        UpJobLog byId = this.getById(id);
        Integer upBaseId = byId.getUpBaseId();
        MeUpJobInfoVo infoVo=new MeUpJobInfoVo();
        UpBase upBase = baseService.getById(upBaseId);
        infoVo.setUpBase(upBase);
        List<UpJobLog> list = this.lambdaQuery()
                .eq(UpJobLog::getUpBaseId, upBaseId)
                .list();
        //排序
        list = list.stream().sorted(Comparator.comparingInt(UpJobLog::getId)).collect(Collectors.toList());
        for(int i=0;i<list.size();i++){
            List<UpProcessLog> processLogList = processLogService.lambdaQuery()
                    .eq(UpProcessLog::getUpJobLogId, list.get(i).getId())
                    .list();
            processLogList = processLogList.stream().sorted(Comparator.comparingInt(UpProcessLog::getId)).collect(Collectors.toList());
            list.get(i).setProcessLogList(processLogList);
        }
        infoVo.setList(list);
        return infoVo;
    }

    @Override
    public Object getCountByFaculty() {
        List<Map<String, Object>> list =  new ArrayList<>();

        QueryWrapper<UpJobLog> qw_wait = new QueryWrapper<>();
        qw_wait.select("faculty,count(*) as waitCount");//查询自定义列
        qw_wait.eq("status","0");
        qw_wait.groupBy("faculty");
        qw_wait.having("count(*) > 1");
        List<Map<String, Object>> waitMap = this.listMaps(qw_wait);

        QueryWrapper<UpJobLog> qw_over = new QueryWrapper<>();
        qw_over.select("faculty,count(*) as overCount");//查询自定义列
        qw_over.eq("status","1");
        qw_over.groupBy("faculty");
        qw_over.having("count(*) > 1");
        List<Map<String, Object>> overMap = this.listMaps(qw_over);

        list.addAll(waitMap);
        list.addAll(overMap);
        return list;
    }


}
//
//    QueryWrapper<CustomerBussUser> qw = new QueryWrapper<>();
//qw.select("CUST_ID,count(*) as custIdCount");//查询自定义列
//        qw.eq("user_id",userId);
//        qw.eq("isvalid","1");
//        qw.groupBy("cust_Id");
//        qw.having("count(*) > 1");
////listMaps方法查询
//        List<Map<String, Object>> maps = customerBussUserService.listMaps(qw);
//
//
//<result column="name" property="name" />
//<result column="userCode" property="userCode" />
//<result column="userName" property="userName" />
//<result column="gender" property="gender" />
//<result column="faculty" property="faculty" />
//<result column="series" property="series" />
//<result column="major" property="major" />
//<result column="upPost" property="upPost" />
//<result column="upLevel" property="upLevel" />